#include "clickedlabel.h"

ClickedLabel::ClickedLabel(QWidget* parent):QLabel(parent),_curstate(ClickLbState::Normal)
{

}

void ClickedLabel::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton){
        if(_curstate==ClickLbState::Normal){
            _curstate=ClickLbState::Selected;
            setProperty("state",_selected_press);
            qDebug()<<"click visible";
            repolish(this);
            update();
        }
        else{
            _curstate=ClickLbState::Normal;
            setProperty("state",_normal_press);
            qDebug()<<"click unvisible";
            repolish(this);
            update();
        }
    }
    QLabel::mousePressEvent(event);
}

void ClickedLabel::enterEvent(QEvent *event)
{
    if(_curstate==ClickLbState::Normal){
        setProperty("state",_normal_hover);
        repolish(this);
        update();
    }
    else{
        setProperty("state",_selected_hover);
        repolish(this);
        update();
    }
    QLabel::enterEvent(event);
}

void ClickedLabel::leaveEvent(QEvent *event)
{
    if(_curstate==ClickLbState::Normal){
        setProperty("state",_normal);
        repolish(this);
        update();
    }
    else{
        setProperty("state",_selected);
        repolish(this);
        update();
    }
    QLabel::leaveEvent(event);
}

void ClickedLabel::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton){
        if(_curstate==ClickLbState::Normal){
            setProperty("state",_normal_hover);
            repolish(this);
            update();
        }
        else{
            setProperty("state",_selected_hover);
            repolish(this);
            update();
        }
        emit clicked(this->text(),_curstate);
        return;
    }

    QLabel::mouseReleaseEvent(event);
}

void ClickedLabel::SetState(QString normal, QString hover, QString press,
                            QString select, QString select_hover, QString select_press)
{
    _normal=normal;
    _normal_hover = hover;
    _normal_press = press;

    _selected = select;
    _selected_hover = select_hover;
    _selected_press = select_press;

    setProperty("state",normal);
    _curstate=ClickLbState::Normal;
    repolish(this);

}

ClickLbState ClickedLabel::GetCurState()
{
    return _curstate;
}

bool ClickedLabel::SetCurState(ClickLbState state)
{
    _curstate=state;
    if(_curstate==ClickLbState::Normal){
        setProperty("state",_normal);
        repolish(this);
    }
    else if(_curstate==ClickLbState::Selected){
        setProperty("state",_selected);
        repolish(this);
    }
    return true;
}

void ClickedLabel::ResetNormalState()
{
    _curstate=ClickLbState::Normal;
    setProperty("state",_normal);
    repolish(this);
}
